package com.ihr360.payroll.service.salaryprofile.convertor;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.entity.staff.CompanySalaryProfileEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileFieldEntity;
import com.ihr360.payroll.model.dto.salary.convertor.SalaryProfileFieldConvertor;
import com.ihr360.payroll.model.dto.salary.profile.CompanySalaryProfile;
import com.ihr360.payroll.model.dto.staff.SalaryProfileField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CompanySalaryProfileConvertor implements IConvertor<CompanySalaryProfileEntity, CompanySalaryProfile> {

    @Autowired
    SalaryProfileFieldConvertor salaryProfileFieldConvertor;

    @Override
    public CompanySalaryProfile convertEntityToDto(CompanySalaryProfileEntity entity) {
        return null;
    }

    @Override
    public CompanySalaryProfileEntity convertDtoToEntity(CompanySalaryProfile dto) {
        CompanySalaryProfileEntity salaryProfileEntity = new CompanySalaryProfileEntity();
        salaryProfileEntity.setId(dto.getId());
        List<SalaryProfileField> profileFieldList = dto.getProfileFields();
        if (CollectionUtils.isEmpty(profileFieldList)) {
            salaryProfileEntity.setProfileFields(Lists.newArrayList());
            return salaryProfileEntity;
        }
        List<SalaryProfileFieldEntity> salaryProfileFieldEntities = Lists.newArrayListWithCapacity(profileFieldList.size());
        profileFieldList.forEach(profileField -> {
            salaryProfileFieldEntities.add(salaryProfileFieldConvertor.convertDtoToEntity(profileField));
        });
        salaryProfileEntity.setProfileFields(salaryProfileFieldEntities);
        return salaryProfileEntity;
    }
}
